﻿using System;
using System.Linq;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class MDIParentForm : Form
    {

        private Account currentAccount = null;
        public MDIParentForm()
        {
            InitializeComponent();

            if (null == currentAccount)
            {
                menuSwitchAccount_Click(null, null);
            }
        }



        public Account CurrentAccount
        {
            get { return currentAccount; }
            set { currentAccount = value;}
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }


        private void menuSwitchAccount_Click(object sender, EventArgs e)
        {

            CloseAllToolStripMenuItem_Click(sender,e);

            FormLogin childForm = new FormLogin();
            childForm.StartPosition = FormStartPosition.CenterParent;
            DialogResult dr = childForm.ShowDialog();

            currentAccount = childForm.currAccount;
            if (null != currentAccount)
            {
                this.Text = "Bike Rent System (" + currentAccount.Role + " - " + currentAccount.LoginId + ")";
            }

            menuAccounts.Enabled = false;
            menuOptions.Enabled = false;
            menuBikes.Enabled = false;
            menuRentedList.Enabled = false;
            menuBillList.Enabled = false;


            if (null != currentAccount)
            {

                switch (currentAccount.Role)
                {
                    case ConstData.ACCOUNT_ROLE_OPERATOR:
                        menuBikes.Enabled = true;
                        menuRentedList.Enabled = true;
                        break;
                    case ConstData.ACCOUNT_ROLE_ADMIN:

                        menuAccounts.Enabled = true;
                        menuOptions.Enabled = true;
                        menuBikes.Enabled = true;
                        break;
                    case ConstData.ACCOUNT_ROLE_BOSS:

                        menuBikes.Enabled = true;
                        menuRentedList.Enabled = true;
                        menuBillList.Enabled = true;
                        break;
                }

            }
        }

        private void menuQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuAccounts_Click(object sender, EventArgs e)
        {
            openChild("FormAccounts");
        }

        private void menuBikes_Click(object sender, EventArgs e)
        {
            openChild("FormBikes");
        }

        private void menuRentedList_Click(object sender, EventArgs e)
        {
            openChild("FormRentals");
        }


        private void menuOptions_Click(object sender, EventArgs e)
        {
            openChild("FormOptions");

        }

        private void menuBillList_Click(object sender, EventArgs e)
        {
            openChild("FormBills");
        }

        private void openChild(string childFormName)
        {
            Form childForm = null;
            if (MdiChildren.Length > 0)
            {
                childForm = MdiChildren.FirstOrDefault(f => f.Name.Equals(childFormName));
            }

            if (null == childForm)
            {
                switch (childFormName)
                {
                    case "FormAccounts":
                        childForm = new FormAccounts();
                        break;
                    case "FormBikes":
                        childForm = new FormBikes();
                        ((FormBikes)childForm).CurrentAccount(currentAccount);
                        break;
                    case "FormRentals":
                        childForm = new FormRentals();
                        ((FormRentals)childForm).CurrentAccount(currentAccount);
                        break;
                    case "FormOptions":
                        childForm = new FormOptions();
                        break;
                    case "FormBills":
                        childForm = new FormBills();
                        break;
                }
                if (null != childForm)
                {
                    childForm.MdiParent = this;
                    childForm.WindowState = FormWindowState.Maximized;
                    childForm.Show();
                }
            }
            else
            {
                childForm.Activate();
            }

        }

        private void menuCloseCurrent_Click(object sender, EventArgs e)
        {
            ActiveMdiChild.Close();
        }

        private void menuRentBike_Click(object sender, EventArgs e)
        {
            FormRental formRental = new FormRental();

            Rental rental = new Rental();
            rental.RentNo = CommonUtils.genrateNextSerialNumber(ConstData.SYSTEM_NEXT_SERIALNUMBER_RENTAL);
            rental.OperatorName = currentAccount.UserName;
            formRental.BindingSource.Add(rental);

            formRental.StartPosition = FormStartPosition.CenterParent;
            formRental.Show();
        }

        private void menuReturnBike_Click(object sender, EventArgs e)
        {
            FormBill formBill = new FormBill();

            formBill.BindingSource.AddNew();

            formBill.StartPosition = FormStartPosition.CenterParent;
            formBill.Show();

        }

        private void menuChangePassword_Click(object sender, EventArgs e)
        {

            FormChangePassword childForm = new FormChangePassword();
            childForm.StartPosition = FormStartPosition.CenterParent;
            childForm.CurrentAccount(currentAccount);
            childForm.ShowDialog();

        }


    }
}
